#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *

class Page:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)


  ## id=row[0] title=row[1] content=row[2] nav_id=row[3]
  def getContent(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_page_where_id % id) == 0):
      sql=Glob.select_all_from_page_where_id % id
      logging.debug(u'Page: getContent() SQL error : %s' % sql)
      return 0
    else:
      result=self.mDB.resultSQL()
      for row in result:
        html_content=row[2]
        self.nav_id=row[3]
        self.title=row[1]
    if html_content:
      return html_content
    else:
      return 0  


  def getTitle(self):
    if self.title:
      return self.title
    else:
      return -1


  def getNavID(self):
    if self.nav_id:
      return self.nav_id
    else:
      return -1


  def setPage(self, id, title, content, nav_id):
    u_content=unicode(content.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    if (self.mDB.executSQL(Glob.update_page_where_id % (u_title, u_content, nav_id, id) ) == 0):
      sql=Glob.update_page_where_id % (u_title, u_content, nav_id, id)
      logging.debug(u'Page: setPage() SQL error : %s' % sql)
      return 0
    else:
      return 1    


  def newPage(self, title, content, nav_id):
    u_content=unicode(content.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')    
    if (self.mDB.executSQL(Glob.insert_page % (u_title, u_content, nav_id) ) == 0):
      sql=Glob.insert_page % (u_title, u_content, nav_id)
      logging.debug(u'Page: newPage() SQL error : %s' % sql)
      return 0
    else: 
      if (self.mDB.executSQL(Glob.last_insert_id_page) == 0):
        logging.debug(u'Page: newPage() SQL error : %s' % Glob.last_insert_id_page)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
         return id 


  def deletePage(self, id):
    if (self.mDB.executSQL(Glob.delete_page_where_id % id) == 0):
      sql=Glob.delete_page_where_id % id
      logging.debug(u'Page: deletePage() SQL error : %s' % sql)
      return 0
    else:
      return 1
